
import { _decorator, Component, instantiate, Label, Node, Animation, Sprite, Vec3, AnimationState, AnimationClip } from 'cc';
import { Loader } from '../../tools/LoadManger';
import { CEventName, DamageStruct } from '../FightControl';

import { Boss } from '../monster/Boss';
import EvtManger from '../../tools/EvtManger';
import { EventName } from '../../manager/AppControll';
import { Tools } from '../../tools/Tools';
import { MonsterManager } from '../monster/MonsterManager';
const { ccclass, property } = _decorator;

@ccclass('Hp')
export class Hp extends Component {
    protected progressBar: Sprite = null;
    protected mNdHitText: Node = null;
    protected _maxHP: number = 100;
    protected _curHP: number = 100;
    protected mNdHp: Node = null;
    protected mIsFinishInit: boolean = false;
    protected onLoad(): void {
        Loader.LoadPrefab("Hp", (prefab) => {
            let nd = instantiate(prefab);
            nd.position = new Vec3(0, 70, 0);
            this.node.addChild(nd);
            this.progressBar = nd.getChildByName("Bar").getComponent(Sprite);
            this.progressBar.fillRange = 1;
            this.mNdHp = nd;
            this.mNdHp.active = false;
            this.mIsFinishInit = true;
        })
    }

    ChangeDirection() {
        if (!this.mNdHp) return;
        if (this.node.scale.x == -1) {
            this.mNdHp.scale = new Vec3(-1, this.node.scale.y, this.node.scale.z);
        } else {
            this.mNdHp.scale = new Vec3(1, this.node.scale.y, this.node.scale.z);
        }
    }

    ShowHp() {
        if (!this.mNdHp) return;
        this.mNdHp.active = true;
    }

    HideHp() {
        if (!this.mNdHp) return;
        this.mNdHp.active = false;
    }

    InitHp(maxHp: number) {
        this._maxHP = maxHp;
        this._curHP = maxHp;
        if (this.mNdHp) {
            this.progressBar.fillRange = this._curHP / this._maxHP;
        }
    }

    public takeDamage(damageS: DamageStruct) {
        if (!this.mIsFinishInit) return;
        if (!this.progressBar) return;
        if (!this.mNdHp) return;
        this.PlaySubtractHealth(damageS)
        this._curHP -= damageS.damage;
        if (this._curHP < 0) {
            this._curHP = 0;
        }
        if (this.mNdHp.active) {
            this.progressBar.fillRange = this._curHP / this._maxHP;
        }
        if (this.node.getComponent("Boss")) {
            EvtManger.do(EventName.showBossHp, this._curHP / this._maxHP);
        }
        if (this._curHP <= 0) {
            this.onDeath();
        }
    }

    PlaySubtractHealth(damageS: DamageStruct) {
        if (!MonsterManager.hitPrefab) {
            console.error("MonsterManager.hitPrefab:未初始化");
            return;
        }
        let nd = MonsterManager.hitNodePool.get();
        if (!nd) {
            console.error("hitPrefab 对象池 容量不足");
            return;
        }

        this.node.parent.addChild(nd);
        nd.position = this.node.position;
        nd.layer = 1 << 25;
        nd.getComponent(Animation).once(Animation.EventType.FINISHED, () => {
            nd.getComponent(Animation).stop();
            nd.getChildByName("numPosition").getChildByName("critDamage").active = false;
            nd.getChildByName("numPosition").getChildByName("normalDamage").active = false;

            MonsterManager.hitNodePool.put(nd);
        })
        nd.setSiblingIndex(10000);

        let randomInt = Math.floor(Math.random() * 3) + 1;
        let temp = nd.getChildByName("numPosition");
        let tempHit = null;
        if (damageS.isCriticalHit) {
            tempHit = temp.getChildByName("critDamage");
        } else {
            tempHit = temp.getChildByName("normalDamage");
        }
        tempHit.active = true;

        tempHit.getChildByName("demageNum").getComponent(Label).string = Tools.FormatNumber(damageS.damage);
        nd.getComponent(Animation).getState("demage00" + randomInt).wrapMode = AnimationClip.WrapMode.Default;
        nd.getComponent(Animation).play("demage00" + randomInt);

    }

    protected onDeath() {
        if (!this.node.active) return;
        if (!this.mNdHp) return;
        this.mNdHp.destroy();
        this.mNdHp = null;
        this.node.emit(CEventName.HpEmpty);
        this.destroy();
        // 怪物死亡逻辑
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
 */
